package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Movie;
import com.bussiness.PersonBussinessProxy;
import com.constants.Constants;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;

/**
 * Servlet implementation class GetCartDetailsServlet
 */
public class GetCartDetailsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PersonBussinessProxy personBI = new PersonBussinessProxy();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetCartDetailsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			HttpSession session = request.getSession(true);
			session.setMaxInactiveInterval(Constants.sessionTimeOut);
			RequestDispatcher dispatcher;
			personBI.setEndpoint(ServiceEndPoints.PersonEndPoint);
			long membershipId;
			if(session.getAttribute("MembershipId") != null)
			{
				try
				{
					membershipId = Long.parseLong(session.getAttribute("MembershipId").toString());
				}
				catch(Exception e)
				{
					session.setAttribute("ServerException", UIMessages.SERVICE_UNAVAILABLE);
					dispatcher = getServletContext().getRequestDispatcher("/cartDetails.jsp");
					dispatcher.forward(request, response);
					return;
				}
				Movie[] moviesList = personBI.customerqueue(membershipId);
				session.setAttribute("cartMovies", moviesList);
			}			
			else
			{
				session.setAttribute("ServerException", UIMessages.SERVICE_UNAVAILABLE);
			}
			dispatcher = getServletContext().getRequestDispatcher("/cartDetails.jsp");
			dispatcher.forward(request, response);
			
		}
		catch(Exception e)
		{
			HttpSession session = request.getSession(true);
			session.setAttribute("ServerException", UIMessages.SERVICE_UNAVAILABLE);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/cartDetails.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
